﻿using Respository.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Dapper.Contrib.Extensions;

namespace Repository.Base.Entity
{
    /// <summary>
    /// 单个表的 增删查改
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RepositoryEntity<T> : RepositoryBase, IRepositoryEntity<T> where T : class,new()
    {
        public bool Delete(T t)
        {
            using (IDbConnection Conn = Respository.SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Delete(t);
            }
        }

        public IEnumerable<T> GetAll()
        {
            using (IDbConnection Conn = Respository.SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.GetAll<T>();
            }
        }

        public T GetById(int id)
        {
            using (IDbConnection Conn = Respository.SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Get<T>(id);
            }
        }

        public long Insert(T t)
        {
            using (IDbConnection Conn = Respository.SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Insert(t);
            }
        }

        public bool Update(T t)
        {
            using (IDbConnection Conn = Respository.SqlServerRespository.SqlServerContext.GetSqlConnection())
            {
                return Conn.Update(t);
            }
        }
    }
}
